package com.joyoung.common.utils.singn;

import java.security.MessageDigest;

public class Md5Coder {
    public Md5Coder() {
    }
    
    public static byte[] encrypt(byte[] data) throws Exception {
        return encrypt(data, (byte[])null, false);
    }
    
    public static byte[] encrypt(byte[] data, int offset, int length) throws Exception {
        return encrypt(data, (byte[])null, false, offset, length);
    }
    
    public static byte[] encrypt(byte[] data, boolean isTwice) throws Exception {
        return encrypt(data, (byte[])null, isTwice);
    }
    
    public static byte[] encrypt(byte[] data, byte[] salt) throws Exception {
        return encrypt(data, salt, false);
    }
    
    public static byte[] encrypt(byte[] data, byte[] salt, boolean isTwice) throws Exception {
        return encrypt(data, salt, isTwice, 0, data.length);
    }
    
    public static byte[] encrypt(byte[] data, byte[] salt, boolean isTwice, int offset, int length) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(data, offset, length);
        if (salt != null && salt.length != 0) {
            md5.update(salt);
        }
        
        byte[] md5Bytes = md5.digest();
        if (isTwice) {
            md5.update(md5Bytes, 8, 8);
            byte[] md52 = md5.digest();
            System.arraycopy(md52, 8, md5Bytes, 8, 8);
        }
        
        return md5Bytes;
    }
}

